package com.ldg.crm.dao;

import com.ldg.crm.dto.UserDto;
import com.ldg.crm.entity.UserEntity;
import com.ldg.crm.entity.UserEntityDto;
import com.ldg.crm.entity.UserEntityExample;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface UserDao {
    
	/****
	 * 登录
	 * @param user
	 * @return
	 */
	@Select("select * from tb_user where user_name=#{userName} and user_pwd=#{userPwd}")
	UserEntity findByNumberAndPassword(UserEntity user);
	
	/**
     *
     * @mbg.generated
     */
    @SelectProvider(type=UserEntitySqlProvider.class, method="countByExample")
    long countByExample(UserEntityExample example);

    /**
     *
     * @mbg.generated
     */
    @DeleteProvider(type=UserEntitySqlProvider.class, method="deleteByExample")
    int deleteByExample(UserEntityExample example);

    /**
     *
     * @mbg.generated
     */
    @Delete({
        "delete from tb_user",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer uid);

    /**
     *useGeneratedKeys:使用主键 uid
     * @mbg.generated
     */
    @Options(useGeneratedKeys = true, keyProperty = "uid")
    @Insert({
        "insert into tb_user (uid, dep_id, ",
        "employee_id, user_name, ",
        "user_pwd, create_date, ",
        "status)",
        "values (#{uid,jdbcType=INTEGER}, #{depId,jdbcType=INTEGER}, ",
        "#{employeeId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, ",
        "#{userPwd,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP}, ",
        "#{status,jdbcType=TINYINT})"
    })
    int insert(UserEntity record);
    
    /**
     *
     * @mbg.generated
     */
    @InsertProvider(type=UserEntitySqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="uid", before=true, resultType=Integer.class)
    int insertSelective(UserEntity record);

    /**
     *
     * @mbg.generated
     */
    @SelectProvider(type=UserEntitySqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="dep_id", property="depId", jdbcType=JdbcType.INTEGER),
        @Result(column="employee_id", property="employeeId", jdbcType=JdbcType.INTEGER),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    List<UserEntity> selectByExample(UserEntityExample example);
    
    @Select("select uid,tb_user.dep_id,crm_employee.`name`,user_name,user_pwd,create_date,tb_user.`status` \r\n" + 
    		"from tb_user,crm_employee " + 
    		"where tb_user.employee_id = crm_employee.employee_id")
   List<UserEntityDto> selectByExample_test();

    /**
     *
     * @mbg.generated
     */
    @Select({
        "select",
        "uid, dep_id, employee_id, user_name, user_pwd, create_date, status",
        "from tb_user",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="dep_id", property="depId", jdbcType=JdbcType.INTEGER),
        @Result(column="employee_id", property="employeeId", jdbcType=JdbcType.INTEGER),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    UserEntity selectByPrimaryKey(Integer uid);
    
    @Select("select count(user_name) " + 
    		"from tb_user " + 
    		"where user_name=#{username}")
    int selectByUserName(@Param("username") String username);

    /**
     *
     * @mbg.generated
     */
    @UpdateProvider(type=UserEntitySqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") UserEntity record, @Param("example") UserEntityExample example);

    /**
     *
     * @mbg.generated
     */
    @UpdateProvider(type=UserEntitySqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") UserEntity record, @Param("example") UserEntityExample example);

    /**
     *
     * @mbg.generated
     */
    @UpdateProvider(type=UserEntitySqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(UserEntity record);

    /**
     *
     * @mbg.generated
     */
    @Update({
        "update tb_user",
        "set dep_id = #{depId,jdbcType=INTEGER},",
          "employee_id = #{employeeId,jdbcType=INTEGER},",
          "user_name = #{userName,jdbcType=VARCHAR},",
          "user_pwd = #{userPwd,jdbcType=VARCHAR},",
          "create_date = #{createDate,jdbcType=TIMESTAMP},",
          "status = #{status,jdbcType=TINYINT}",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(UserEntity record);
}